#pragma once

#include"../sql/campus_sql_connector.h"
#include"../net/campus_whole_server.h"

struct CampusClubInfo;

using json = nlohmann::json;
class CampusClubInfoManager
{
public:
    CampusClubInfoManager(CampusWholeServer* whole_server, CampusSqlConnector* sql_connector);
    ~CampusClubInfoManager();

    // -------------------------- 业务回调接口（供WholeManager转发）--------------------------
    void handleClubCreate(websocketpp::connection_hdl hdl, json JMsg);
    void handleClubSearch(websocketpp::connection_hdl hdl, json JMsg);
    void handleClubJoin(websocketpp::connection_hdl hdl, json JMsg);
    void handleClubGroup(websocketpp::connection_hdl hdl, json JMsg);
    void handleClubInfo(websocketpp::connection_hdl hdl, json JMsg);
    void handleGroupChat(websocketpp::connection_hdl hdl, json JMsg);

    // -------------------------- 数据库回调接口 供WholeManager转发）--------------------------
    void onSelectSucess(std::vector<std::vector<std::pair<std::string, std::string>>>& result, CampusSqlInfo* userData);
    void onSelectFail(CampusSqlInfo* userData);

    void onInsertSucess(CampusSqlInfo* userData);
    void onInsertFail(CampusSqlInfo* userData);

    void onUpdateSucess(CampusSqlInfo* userData);
    void onUpdateFail(CampusSqlInfo* userData);

private:   
    void doSelectClub(websocketpp::connection_hdl hdl, json JMsg);
    void doSelectSearch(websocketpp::connection_hdl hdl, json JMsg);
    void doSelectJoin(websocketpp::connection_hdl hdl, json JMsg);
    void doSelectChat(websocketpp::connection_hdl hdl, json JMsg);

    void doClubExist(std::shared_ptr<CampusClubInfo> p_info);
    void doInsertClubInfo(std::shared_ptr<CampusClubInfo> p_info);

    void doJoinExist(std::shared_ptr<CampusClubInfo> p_info);
    void doInsertClubJoin(std::shared_ptr<CampusClubInfo> p_info);

    void doSelectClubInfo(websocketpp::connection_hdl hdl, json JMsg);
    void doSelectClubGroup(websocketpp::connection_hdl hdl, json JMsg);

    void doSendChatInfo(std::string const& info,std::vector<std::string>const& uid_list,std::string const& sender_uid,std::string const& club_name,int serial);

    void handleClubCreateSelected(std::vector<std::vector<std::pair<std::string, std::string>>>& result, std::shared_ptr<CampusClubInfo> p_info);
    void handleClubCreateInserted(std::shared_ptr<CampusClubInfo> p_info);
    void handleClubSearchSelected(std::vector<std::vector<std::pair<std::string, std::string>>>& result, std::shared_ptr<CampusClubInfo> p_info);
    void handleClubJoinSelected(std::vector<std::vector<std::pair<std::string, std::string>>>& result, std::shared_ptr<CampusClubInfo> p_info);
    void handleClubJoinInserted(std::shared_ptr<CampusClubInfo> p_info);
    void handleClubGroupSelected(std::vector<std::vector<std::pair<std::string, std::string>>>& result, std::shared_ptr<CampusClubInfo> p_info);
    void handleClubInfoSelected(std::vector<std::vector<std::pair<std::string, std::string>>>& result, std::shared_ptr<CampusClubInfo> p_info);
    void handleClubChatSelected(std::vector<std::vector<std::pair<std::string, std::string>>>& result, std::shared_ptr<CampusClubInfo> p_info);

private:
    CampusWholeServer* whole_server_;                  // 服务器实例（用于发送消息）
    CampusSqlConnector* sql_connector_;// SQL连接实例（用于数据库操作）
};